<template>
	<view>
		<block>
			<uni-nav-bar @clickLeft="goBack" status-bar="true" shadow="true" left-icon="back" left-text="返回" title="手机登录"></uni-nav-bar>
		</block>
		<view class="uni-form-item " >
			<view class="title">手机号</view>
			<input v-model="temphone" @focus="getFocus(0)" class="uni-input " v-bind:class="{ 'input-error': hasError[0] }"
			 placeholder="请输入手机号" />
		</view>
		<view class="uni-form-item " >
			<view class="title">验证码</view>
			<input v-model="password" @focus="getFocus(1)" v-bind:class="{ 'input-error': hasError[1] }" class="uni-input"
			 placeholder="请输入验证码" />
		</view>
		<button class="button" type="primary" @click="goLogin">登录</button>
	</view>
</template>

<script>
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import config from "@/common/config.js"
	export default {
		components: {
			uniNavBar
		},
		data() {
			return {
				temphone: '',
				password: '',
				hasError: [false, false],
				sessionkey:''
			}
		},
		methods: {
			
			toLogin(){
				if (this.temphone.length == 0) {
					this.hasError.splice(0, 1, true);
					return;
				}
				if (this.password.length == 0) {
					this.hasError.splice(1, 1, true);
					return;
				}
				var url = config.baseUrl + '/users/login';
				var name = this.temphone;
				var password = this.password;
				var sessionkey  = this.sessionkey;
				var data = {
					name,
					password,
					sessionkey
					
				};
				uni.request({
					method: 'POST',
					url: url,
					data: data,
					header: {
						'content-type': 'application/json',
					},
					success: (res) => {
						// console.log(res);
						var statusCode = res.statusCode;
						console.log(statusCode);
						if (200 == statusCode) {
							//保存token
							console.log(res.data);
							uni.setStorageSync(config.token, res.data.token);
							uni.setStorageSync('telephone', res.data.thirdBindNew.telephone);
							uni.setStorageSync('nickname', res.data.thirdBindNew.nickname);
							uni.setStorageSync('avatarurl', res.data.thirdBindNew.avatarurl);
							// //用其他账号登录后，便于重新绑定
							// var localTelephone = uni.getStorageSync(config.telephoneKey);
							// if(localTelephone){
							// 	if(!(localTelephone==this.temphone)){
							// 		console.log('logcla::'+uni.getStorageSync(config.telephoneKey));
							// 		console.log('logclattt::'+config.telephoneKey)
							// 		uni.removeStorageSync(config.sessionkey);
							// 		uni.setStorageSync(config.telephoneKey,this.temphone);
							// 	}
							// }else{
							// 	uni.setStorageSync(config.telephoneKey,this.temphone);
							// }
							uni.showToast({
								title: "登录成功",
								duration: 2000,
							});
							uni.switchTab({
							    url: '../wode/wode'
							});
						} else {
							uni.showToast({
								title: "else:"+url,
								duration: 5000,
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "登录失败f"
						});
					}
				});
			},
			//账号密码登录，//登录/注册， 并且做了绑定微信（登录之前必须授权微信登录，才能用sessionkey）
			goLogin() {
				uni.checkSession({
					success: (e) => {
						this.sessionkey = uni.getStorageSync(config.sessionkey);
						if (this.sessionkey.length != 0) {
							console.log("wei_guo_qi==" + this.sessionkey);
							this.toLogin();
						}else{
							//跳转到授权页面
							uni.navigateTo({
								url: '../authorityRequst/authorityRequst?goto=loginbytelephone'
							})
							
						}
					},
					fail: () => {
						console.log("yi_guo_qi_ session")
						uni.showToast({
							title: "sessionkey 已经过期"
						});
						//跳转到授权页面
						uni.navigateTo({
							url: '../authorityRequst/authorityRequst?goto=loginbytelephone'
						})
					}
				});
			},
			getFocus(indxe) {
				this.hasError.splice(indxe, 1, false);
			},
			goBack() {
				uni.navigateBack({
			
				})
			}
		}
	}
</script>

<style>
	.input-error {
		border: 2upx solid #FF3333;
	}
	.title{
		font-size: medium;
	}
	.uni-input{
		font-size: medium;
	}
</style>
